package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/9 17:28
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public List<User> findUserByNameAndSexAndBirthday() {
        //1.创建查询条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //2.设置根据姓名模糊查询条件
        queryWrapper.like("name","王");
        //3.设置根据性别精确查询条件
        queryWrapper.eq("sex","男");
        //4.设置根据日期范围查询条件
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse("2021-02-01 08:04:59");
            queryWrapper.ge("birthday",date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //5.调用dao查询并返回结果
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public void updateUserById(Integer id, User user) {
        userMapper.updateById(user);
    }

    @Override
    public void deleteUserById(Integer id) {
        userMapper.deleteById(id);
    }
}
